package org.whatever.aha.zjut.platform.controller.school;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.whatever.aha.zjut.base.constant.AuthConst;
import org.whatever.aha.zjut.base.constant.ErrorCode;
import org.whatever.aha.zjut.base.dto.AjaxResult;
import org.whatever.aha.zjut.base.exception.AppException;
import org.whatever.aha.zjut.platform.entity.competition.AcademyCompetition;
import org.whatever.aha.zjut.platform.service.UserService;
import org.whatever.aha.zjut.platform.service.school.JudgeProjectDistributionService;
import org.whatever.aha.zjut.platform.vo.school.ProjectArrangementVo;
import org.whatever.aha.zjut.platform.vo.school.TagInfoVo;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Vc
 * @version 1.0
 * @Desc 评委账号相关操作类
 * @date 2021/11/21 16:22
 */
@Api(tags = "评委账号相关操作类")
@RequestMapping("/v1/school/operateJudgeAccount")
@RequiredArgsConstructor
@RestController
@Validated
@SaCheckRole(value = {AuthConst.R_supper, AuthConst.R_school}, mode = SaMode.OR)
public class JudgeAccountController {
    final UserService userService;
    final JudgeProjectDistributionService judgeProjectDistributionService;

    @ApiOperation("创建评委")
    @PostMapping("/createJudgeAccount")
    public AjaxResult<Integer> createJudgeAccount(@RequestParam(value = "password", required = true)String password,
                                                  @RequestParam(value = "userName", required = true)String userName,
                                                  @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
                                                  @JsonFormat(
                                                          pattern = "yyyy-MM-dd HH:mm:ss",
                                                          timezone = "GMT+8"
                                                  )
                                                  @RequestParam(value = "ddl", required = true)Date ddl,
                                                  @RequestParam(value = "phone", required = false)String phone){
        //重复创建评委检查，手机号是否重复,用户名是否重复,空值不限制
        if(!StringUtils.isEmpty(phone) && userService.getAllPhones().contains(phone)){
            throw new AppException(ErrorCode.PHONE_NUMBER_USED);
        }
        if(!StringUtils.isEmpty(userName) && userService.getAllJudgeNames().contains(userName)){
            throw new AppException(ErrorCode.USERNAME_USED);
        }
        return AjaxResult.SUCCESS(userService.createJudge(password, userName, ddl, phone));
    }

    @ApiOperation("指派评审赛事")
    @PostMapping("/arrangeProject")
    public AjaxResult<Integer> arrangeProject(@RequestParam(value = "userId", required = true)int userId,
                                              @RequestParam(value = "compId", required = true)int compId,
                                              @RequestParam(value = "stageId", required = true)int stageId,
                                              @RequestParam(value = "tag", required = true)String tag){
        //修改评委信息应该把修改组别做到同一个界面，故遇到重复的比赛评委、tag直接跳过不报错
        if (judgeProjectDistributionService.getTagsByCompIdAndStageIdAndJudgeId(compId, stageId, userId).contains(tag)) {
//            throw new AppException(ErrorCode.JUDGE_ALREADY_ARRANGED_BY_THE_TAG);
            return AjaxResult.SUCCESS(1);
        }
        return AjaxResult.SUCCESS(judgeProjectDistributionService.arrangeProject(userId, compId, stageId, tag));
    }

    @ApiOperation("修改评委信息")
    @PutMapping("/modifyJudgeAccount")
    public AjaxResult<Integer> modifyJudgeAccount(@RequestParam(value = "userId", required = true)int userId,
                                                  @RequestParam(value = "password", required = false)String password,
                                                  @RequestParam(value = "userName", required = false)String userName,
                                                  @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
                                                      @JsonFormat(
                                                              pattern = "yyyy-MM-dd HH:mm:ss",
                                                              timezone = "GMT+8"
                                                      )
                                                  @RequestParam(value = "ddl", required = false)Date ddl,
                                                  @RequestParam(value = "phone", required = false)String phone){
        return AjaxResult.SUCCESS(userService.modifyJudgeAccount(userId, password, userName, ddl, phone));
    }

    @ApiOperation("删除评委信息")
    @DeleteMapping("/deleteJudgeAccount")
    public AjaxResult<Integer> deleteJudgeAccount(@RequestParam(value = "userId", required = true)int userId){
        judgeProjectDistributionService.deleteJudgeAccount(userId);
        return AjaxResult.SUCCESS(userService.deleteJudgeAccount(userId));
    }

    @ApiOperation("项目评审分配概览")
    @GetMapping("/projectArrangement")
    public AjaxResult<List<ProjectArrangementVo>> projectArrangement(@RequestParam(value = "compId", required = true)int compId){
        return AjaxResult.SUCCESS(judgeProjectDistributionService.projectArrangement(compId));
    }

    @ApiOperation("获取未被分配的组别信息")
    @GetMapping("/getUnArrangeTagInfo")
    public AjaxResult<List<TagInfoVo>> getUnArrangeTagInfo(@RequestParam(value = "compId", required = true)int compId){
        return AjaxResult.SUCCESS(judgeProjectDistributionService.getUnArrangeTagInfo(compId));
    }

}
